<template>
  <view class="page-continer" style="height: 100%; position: relative">
    <!-- 头部 -->
    <top-nav class="nav-container" />
    <view class="chat-background">
      <!-- 消息列表组件 -->
      <message />
      <!-- 输入框/长按语音组件 -->
      <textchat class="msg-input" />
    </view>
  </view>
</template>

<script setup>
import topNav from "./coms/topNav.vue"; //引入组件
import message from "./coms/message.vue";
import textchat from "./coms/textchat.vue";
import { onLoad, onShow, onHide, onUnload } from "@dcloudio/uni-app";
import { getAgent } from "@/global/agentApi";
import useVoice from "@/store/useVoice.js";
import useAgent from "@/store/useAgent.js";
import useWs from "@/store/useWs.js";
import useChat from "@/store/useChat.js";

const useChatStore = useChat();
const useWsStore = useWs();
const useVoiceStore = useVoice();
const useAgentStore = useAgent();
let windowHeight = ref(0);
let windowWidth = ref(0);
let options = ref({});
onHide(() => {
  useWsStore.onCloseVoiceChat();
});
//页面销毁 暂停录音
onUnload(() => {
  useWsStore.onCloseVoiceChat();
});
onShow(() => {
  useVoiceStore.chatType = 1; //暂存聊天类型
  useChatStore.clientReplayText = "";
  if (useWsStore._ws.isConnectSucc) {
    useWsStore.onWsOpen();
  }
});

onLoad((option) => {
  //回到首页发送标识 可以打断ws推送消息
  //获取智能体详情，存到pinia
  getAgent({
    agentId: useAgentStore.agentId,
  }).then((res) => {
    useAgentStore.agentInfo = res.data;
    useWsStore.initWs();
  });

  options.value = option;
  uni.setNavigationBarTitle({
    title: option.nickname,
  });
  uni.getSystemInfo({
    success: (info) => {
      windowHeight.value = info.windowHeight; // 可使用窗口高度
      windowWidth.value = info.windowWidth; // 可使用窗口宽度
    },
  });
});
</script>

<style lang="scss" scoped></style>
